
/*查询数据*/
function supplementaryInvoice() {
    if($("#order_id").val().length>20){
        $.Huimodalalert('订单号长度不能超过20',2000);
        return false;
    }
    if($("#customer_name").val().length>50){
        $.Huimodalalert('客户名称长度不能超过50',2000);
        return false;
    }
    $.ajax({
        url:root.orderInter.queryBackgroundSupplementInvoice,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "page_size":"10",
            "page_num":"1",
            "order_id":$("#order_id").val(),
            "customer_name":$("#customer_name").val(),
            "order_status":$("#order_status").val(),
            "platform":crmCustomerIdOnearry,
            "companys_ids":sessionStorage.getItem("organizationWarehouse"),//公司对应的发货仓id
            "warehouse_ids":sessionStorage.getItem("regions"),//仓库权限串
            "belong_groupIds":sessionStorage.getItem("companys"),//销售归属权限串
            "sales_manager_ids":sessionStorage.getItem("personnels"),//销售经理权限串
        }),
        success: function (data) {
            homePagelist(data.data, 0);
            var totalPages=""
            if(data.data!=null&&data.data!=""){
                totalPages=  data.data.totalPage
            }else {
                totalPages=0
            }
            $('.M-box3').pagination({
                pageCount: totalPages,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url:root.orderInter.queryBackgroundSupplementInvoice,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "page_size":"10",
                            "page_num":api.getCurrent(),
                            "order_id":$("#order_id").val(),
                            "customer_name":$("#customer_name").val(),
                            "order_status":$("#order_status").val(),
                            "platform":crmCustomerIdOnearry,
                            "companys_ids":sessionStorage.getItem("organizationWarehouse"),//公司对应的发货仓id
                            "warehouse_ids":sessionStorage.getItem("regions"),//仓库权限串
                            "belong_groupIds":sessionStorage.getItem("companys"),//销售归属权限串
                            "sales_manager_ids":sessionStorage.getItem("personnels"),//销售经理权限串
                        }),
                        success: function (result) {
                            homePagelist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
/*表格*/
var dataSource = new Array();
//table的数据源
var table;
dataSource = [];
function homePagelist(data, index){
    dataSource.splice(0,dataSource.length);//清空数组
    if(data!=null && data !=""){
        var datashu =data.startIndex+1;
        $.each(data.result, function (i, field) {

            var datashus = datashu++
            var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.order_id);
            rowData.push(field.platform);
            rowData.push(field.customer_name);
            rowData.push(field.order_status);
            rowData.push(field.created_at);
            rowData.push(field.source);
            rowData.push(field.paid_type);
            rowData.push(field.sent_type);
            rowData.push(field.member_id);
            rowData.push(field.customer_id);
            dataSource.push(rowData);

        });
    }


    initData(dataSource);
    function initData(dataSource) {
        var dttable = $('#openScreentable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#openScreentable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[2] == 10) {
                            val = '普通平台'
                        }else if (row[2] == 20) {
                            val = '签约平台'
                        }else if (row[2] == 40) {
                            val = '对接平台'
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[4] == 70) {
                            val = '已收货待结算'
                        }else if (row[4] == 80) {
                            val = '订单成功'
                        }else if (row[4] == 90) {
                            val = '交易完成'
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var source = "";
                        switch (row[6]) {
                            case 10:
                                source = "安卓";
                                break;
                            case 20:
                                source = "IOS";
                                break;
                            case 30:
                                source = "H5";
                                break;
                            case 40:
                                source = "PC";
                                break;
                            case 50:
                                source = "PC代客下单";
                                break;
                            case 60:
                                source = "综合平台";
                                break;
                            case 70:
                                source = "门店触摸屏";
                                break;
                        }
                        var val = '<span>' + source + '</span>';
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var payWay = "";
                        switch (row[7]) {
                            case 1001:
                                payWay = "在线支付";
                                break;
                            case 1002:
                                payWay = "支付宝支付";
                                break;
                            case 1003:
                                payWay = "微信支付";
                                break;
                            case 1101:
                                payWay = "货到付款";
                                break;
                            case 1102:
                                return "货到付款-现金";
                                break;
                            case 1103:
                                return "货到付款-pos机";
                                break;
                            case 1201:
                                payWay = "账期支付";
                                break;
                            case 1301:
                                payWay = "银行转账";
                                break;
                            case 1401:
                                payWay = "银行汇票";
                                break;
                            case 1601:
                                payWay = "积分支付";
                                break;
                            case 1701:
                                payWay = "门店付款";
                                break;
                        }
                        var val = '<span>' + payWay + '</span>';
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var senttype = "";
                        switch (row[8]) {
                            case 10:
                                senttype = "快递";
                                break;
                            case 20:
                                senttype = "快运";
                                break;
                            case 21:
                                senttype = "快运";
                                break;
                            case 30:
                                senttype = "专车包车";
                                break;
                            case 40:
                                senttype = "鑫方盛物流";
                                break;
                            case 50:
                                senttype = "上门自提";
                                break;
                            case 52:
                                senttype = "配送客户自提";
                                break;

                        }
                        var val = '<span>' + senttype + '</span>';
                        return val;
                    },
                    "targets": 8
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '<span class="powerButton 604"><a class="ml-10" onclick="Pagedetails(\'补开发票详情\',\'supplementaryInvoice_details.html\',\''+row[1]+'\',\''+row[2]+'\',\''+row[9]+'\',\''+row[10]+'\',\''+"详情"+'\')">查看详情</a></span><span class="powerButton 605"><a class="ml-10" onclick="Pagedetails(\'补开发票\',\'supplementaryInvoice_page.html\',\''+row[1]+'\',\''+row[2]+'\',\''+row[9]+'\',\''+row[10]+'\',\''+"发票"+'\')">补开发票</a></span>'
                        return val;
                    },
                    "targets":9
                },

            ]
        });
        //权限控制函数
        power()
    };
}
//名称
function over(id) {
    $("#mouseBox"+id).css({
        display: "inline"
    });
}
function out(id) {
    $("#mouseBox"+id).css({
        display: "none"
    });
}
/*跳转引导页防止多窗口打开*/
function Pagedetails(title,url,data,platform,memberid,customerid,stare){
    sessionStorage.setItem("stare",stare)
    sessionStorage.setItem("supplementOrderid",data);
    sessionStorage.setItem("supplementPlatform",platform);
    sessionStorage.setItem("supplementmemberid",memberid);
    sessionStorage.setItem("supplementcustomerid",customerid);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


//删除
function carouselSettingsdelete(val) {
    var cu = layer.confirm('<span>确定要删除吗？</span>', {title:'提示',btn: ['确认', '取消']}, function(index){
        $.ajax({
            url: root.appversioncontrol.deleteAppScreenMsg,
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify({
                "id":val,
            }),
            success: function (data) {
                if(data.errorCode==0){
                    $.Huimodalalert('删除成功',2000);
                    queryopenScreen()
                }else {
                    $.Huimodalalert(data.msg,2000);
                }

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
        layer.close(cu);
    },function(){
        layer.close(cu);

    });
}
//订单来源
var placeholder02html =[];//存放多选时的html
var placeholder02htmlinit =[];
var crmCustomerIdOnearry =[];//存放多选时的crmCustomerid
var spanhtml = '';
var crmCustomerIdOne='';
$(".ulbox").css('display','none');
/*点击 项目 显示下拉 选项,其中全选全不选，要做传值的处理*/
$(".dataval,.icon_arrow").on('click',function (e) {
    $(this).parent().children(".icon_arrow").toggleClass("icon_arrow_down");
    if($('.ulbox').css("display") == "none"){
        $('.ulbox').show();
        if($(".placeholder02").find(".all-radius")&&$(".placeholder02").find(".all-radius").html()=="全部"){
            $(".placeholder02").html("")

        }
    }else{
        $(".ulbox").hide();
        if ($(".placeholder02").html()==""){
            $(".placeholder02").html('<span class="all-radius">全部</span>')
        }
    }
    /*------------------8.24 如果已经有选择过的--------------------*/
    var initcustomerId =[];
    placeholder02htmlinit=[]
    $.each($(".lidata"),function (index,itm) {

        initcustomerId.push($(itm).attr('crmCustomerId'))
        placeholder02htmlinit.push($(itm).attr('crmCustomername'))
    })
    if(crmCustomerIdOnearry.length >0){
        function isArray(str){
            return Object.prototype.toString.call(str) == "[object Array]";
        }
        if(isArray(crmCustomerIdOnearry)){
            crmCustomerIdOnearry= [crmCustomerIdOnearry].join().split(',');
        }else{
            crmCustomerIdOnearry= [crmCustomerIdOnearry].join().split(',');
        }
        console.log(crmCustomerIdOnearry)
        $.each($(".lidata"),function (index,itm) {
            $.each(crmCustomerIdOnearry,function (i,item) {
                $.each(initcustomerId,function (j,ite) {
                    if(item == ite){
                        if($("itm").attr('crmcustomerid')==item){
                            $(".lidata").find('.evetyipt').prop('checked',true);
                        }
                    }
                })
            })
        })

    }
    e.stopPropagation();
});
var checkednum=0;
//当点击下拉数据时 回显在input里面
var htmlspan='';
$(".ulbox").on('click','input.evetyipt',function (e) {
    if($(".placeholder02").find(".all-radius")&&$(".placeholder02").find(".all-radius").html()=="全部"){
        $(".placeholder02").html("")

    }
    e.stopPropagation();
    if($(this).prop('checked')){
        $(this).prop('checked',true);//选中
        checkednum ++;
        spanhtml=$(this).next().html();
        placeholder02html.push(spanhtml);
        crmCustomerIdOne = $(this).parent().attr('crmCustomerId');
        crmCustomerIdOnearry.push(crmCustomerIdOne);
        $.each(placeholder02html,function(i,item){
            htmlspan = '<span>'+item+'</span>'
        })
        $(".placeholder02").append(htmlspan);
        if($(".ulbox").find('input.evetyipt').length == checkednum){
            $(".checkboxall").prev().prop('checked',true);
        }
        //全选反选时会用
        if (checkednum==0) {
            $(".checkboxall").prev().prop('checked',true);

        }
        //判断如果只选择了一个下拉选项 在input 里面应该展示全部的名称
        onenameadd()
    }else{
        checkednum --;
        $(this).prop('checked',false);
        var checkfalsehtml =  $(this).next().html();
        var tt =$(this).parent().attr('crmCustomerId');
        for (var i=0;i<crmCustomerIdOnearry.length;i++){
            if(crmCustomerIdOnearry[i] == tt){
                crmCustomerIdOnearry.splice(crmCustomerIdOnearry.indexOf(tt),1);
            }
        }
        var iptspandata = $('.placeholder02 span');//点击选中回显在span 里面的值
        $.each(iptspandata,function (i,item) {
            if($(item).html() == checkfalsehtml){
                $(item).html('');
                $(item).remove();
            }
        });
        placeholder02html.splice($(this).next().html(),1);
        if($(".ulbox").find('input.evetyipt').length != checkednum){
            $(".checkboxall").prev().prop('checked',false);
        }
        onename();
    }
});
//可点击空白处
$(document).click(function() {
    $(".ulbox").css('display','none');
    if ($(".placeholder02").html()==""){
        $(".placeholder02").html('<span class="all-radius">全部</span>')
    }
});
function onenameadd(){
    //判断如果只选择了一个下拉选项 在input 里面应该展示全部的名称
    if(crmCustomerIdOnearry.length == 1){
        $(".placeholder02 span").css('max-width','none');
        $(".placeholder02 span").attr('title',$(".placeholder02 span").html());
    }else{
        $(".placeholder02 span").css('max-width','48px');
    }
}
function onename(){
    //判断如果只选择了一个下拉选项 在input 里面应该展示全部的名称
    if(crmCustomerIdOnearry.length == 1){
        $(".placeholder02 span").css('max-width','none');
        $(".placeholder02 span").attr('title',$(".placeholder02 span").html());
    }
}

/*点击全选 联动 传值*/
var ulboxli = $(".ulbox").find('li');
$(".checkboxall").parent().on('click','input',function (e) {
    e.stopPropagation();
    if($(this).prop('checked')){//全选
        $(this).prop('checked',true);
        ulboxli.find('input').prop('checked',true);
        $(".placeholder02").html("")
        placeholder02html =placeholder02htmlinit;
        var htmlspan='';
        $.each(placeholder02html,function(i,item){
            htmlspan += '<span>'+item+'</span>'
        });
        var crmCustomerIds=$(this).attr('crmCustomerId')
        crmCustomerIdOnearry=crmCustomerIds.split(",")
        $(".placeholder02").append(htmlspan);
    }else{//全不选
        $(this).prop('checked',false);
        ulboxli.find('input').prop('checked',false);
        crmCustomerIdOnearry=[];
        placeholder02html=[];
        $(".placeholder02").html('');
    }
});
$(".ulbox").on('click','.checkboxall',function (e) {
    e.stopPropagation();
    if(  $(this).parent().find('input').prop('checked') == false){//全选
        $(this).parent().find('input').prop('checked',true);
        ulboxli.find('input').prop('checked',true);
        placeholder02html =placeholder02htmlinit;
        var htmlspan='';
        $.each(placeholder02html,function(i,item){
            htmlspan += '<span>'+item+'</span>'
        });
        $(".placeholder02").append(htmlspan);
    }else{//全不选
        $(this).parent().find('input').prop('checked',false);
        ulboxli.find('input').prop('checked',false);
        crmCustomerIdOnearry=[];
        placeholder02html=[];
        $(".placeholder02").html('');
    }
})



